package cn.lg.soar.database.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.util.Collection;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2023/11/21 9:42
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class PlusLambdaQueryWrapper<T> extends LambdaQueryWrapper<T> {

    @Override
    public PlusLambdaQueryWrapper<T> like(SFunction<T, ?> column, Object val) {
        super.like(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> likeRight(SFunction<T, ?> column, Object val) {
        super.likeRight(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> likeLeft(SFunction<T, ?> column, Object val) {
        super.likeLeft(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> notLike(SFunction<T, ?> column, Object val) {
        super.notLike(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> eq(SFunction<T, ?> column, Object val) {
        super.eq(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> ne(SFunction<T, ?> column, Object val) {
        super.ne(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> ge(SFunction<T, ?> column, Object val) {
        super.ge(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> le(SFunction<T, ?> column, Object val) {
        super.le(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> gt(SFunction<T, ?> column, Object val) {
        super.gt(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> lt(SFunction<T, ?> column, Object val) {
        super.lt(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> between(SFunction<T, ?> column, Object start, Object end) {
        if (valuable(start)) {
            if (valuable(end)) {
                super.between(true, column, start, end);
                return this;
            }
            super.ge(true, column, start);
            return this;
        }
        if (valuable(end)) {
            super.le(true, column, end);
            return this;
        }
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> notBetween(SFunction<T, ?> column, Object start, Object end) {
        if (valuable(start)) {
            if (valuable(end)) {
                super.notBetween(true, column, start, end);
                return this;
            }
            super.lt(true, column, start);
            return this;
        }
        if (valuable(end)) {
            super.gt(true, column, end);
            return this;
        }
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> in(SFunction<T, ?> column, Object ...val) {
        super.in(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> in(SFunction<T, ?> column, Collection<?> val) {
        super.in(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> notIn(SFunction<T, ?> column, Object ...val) {
        super.notIn(valuable(val), column, val);
        return this;
    }

    @Override
    public PlusLambdaQueryWrapper<T> notIn(SFunction<T, ?> column, Collection<?> val) {
        super.notIn(valuable(val), column, val);
        return this;
    }


    private boolean valuable(Object val) {
        if (val == null) {
            return false;
        }
        if (val instanceof CharSequence) {
            return ((CharSequence) val).length() != 0;
        }
        return true;
    }

    private boolean valuable(Object[] val) {
        return val != null && val.length != 0;
    }

    private boolean valuable(Collection<?> val) {
        return val != null && !val.isEmpty();
    }

}
